package com.ruoyi.financial.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.financial.mapper.FOutboundMapper;
import com.ruoyi.financial.domain.FOutbound;
import com.ruoyi.financial.service.IFOutboundService;
import com.ruoyi.common.core.text.Convert;

/**
 * 出库管理Service业务层处理
 * 
 * @author xunzhe
 * @date 2024-01-31
 */
@Service
public class FOutboundServiceImpl implements IFOutboundService 
{
    @Autowired
    private FOutboundMapper fOutboundMapper;

    /**
     * 查询出库管理
     * 
     * @param outId 出库管理主键
     * @return 出库管理
     */
    @Override
    public FOutbound selectFOutboundByOutId(Long outId)
    {
        return fOutboundMapper.selectFOutboundByOutId(outId);
    }

    /**
     * 查询出库管理列表
     * 
     * @param fOutbound 出库管理
     * @return 出库管理
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<FOutbound> selectFOutboundList(FOutbound fOutbound)
    {
        return fOutboundMapper.selectFOutboundList(fOutbound);
    }

    /**
     * 新增出库管理
     * 
     * @param fOutbound 出库管理
     * @return 结果
     */
    @Override
    public int insertFOutbound(FOutbound fOutbound)
    {
        fOutbound.setCreateTime(DateUtils.getNowDate());
        if(fOutbound.getOutTime() == null){
            fOutbound.setOutTime(DateUtils.getNowDate());
        }
        return fOutboundMapper.insertFOutbound(fOutbound);
    }

    /**
     * 修改出库管理
     * 
     * @param fOutbound 出库管理
     * @return 结果
     */
    @Override
    public int updateFOutbound(FOutbound fOutbound)
    {
        fOutbound.setUpdateTime(DateUtils.getNowDate());
        return fOutboundMapper.updateFOutbound(fOutbound);
    }

    /**
     * 批量删除出库管理
     * 
     * @param outIds 需要删除的出库管理主键
     * @return 结果
     */
    @Override
    public int deleteFOutboundByOutIds(String outIds)
    {
        return fOutboundMapper.deleteFOutboundByOutIds(Convert.toStrArray(outIds));
    }

    /**
     * 删除出库管理信息
     * 
     * @param outId 出库管理主键
     * @return 结果
     */
    @Override
    public int deleteFOutboundByOutId(Long outId)
    {
        return fOutboundMapper.deleteFOutboundByOutId(outId);
    }

    public int selectCountByDate(Map map){
        return fOutboundMapper.selectCountByDate(map);
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    public List<FOutbound> selectFOutboundKucun(FOutbound fOutbound){
        return fOutboundMapper.selectFOutboundKucun(fOutbound);
    }

    public int examine(FOutbound fOutbound)
    {
        return fOutboundMapper.updateFOutbound(fOutbound);
    }

    public String selectAverageSunhao(FOutbound fOutbound){
        Map map = fOutboundMapper.selectAverageSunhao(fOutbound);
        String shuliangStr = map.get("shuliang".toUpperCase()).toString();
        if(shuliangStr.equals("0") || map.get("pingjun_sunhao".toUpperCase())==null || map.get("pingjun_sunhao".toUpperCase()).toString().equals("")){
            return "";
        }else{
            double sumPingjunSunhao = Double.parseDouble(map.get("pingjun_sunhao".toUpperCase()).toString());
            double shuliang = Double.parseDouble(map.get("shuliang".toUpperCase()).toString());
            double pingjunSunhao = sumPingjunSunhao/shuliang;
            return  String.format("%.1f", pingjunSunhao);
        }
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    public List<FOutbound> selectFOutboundTongji(FOutbound fOutbound){
        return fOutboundMapper.selectFOutboundTongji(fOutbound);
    }

}
